<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Vector Layers &mdash; Python GDAL/OGR Cookbook 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Python GDAL/OGR Cookbook 1.0 documentation" href="index.html" />
    <link rel="next" title="Raster Layers" href="raster_layers.html" />
    <link rel="prev" title="Geometry" href="geometry.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="raster_layers.html" title="Raster Layers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="geometry.html" title="Geometry"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="vector-layers">
<h1>Vector Layers<a class="headerlink" href="#vector-layers" title="Permalink to this headline">¶</a></h1>
<div class="section" id="delete-a-file">
<h2>Delete a file<a class="headerlink" href="#delete-a-file" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">DriverName</span> <span class="o">=</span> <span class="s">&quot;ESRI Shapefile&quot;</span>      <span class="c"># e.g.: GeoJSON, ESRI Shapefile</span>
<span class="n">FileName</span> <span class="o">=</span> <span class="s">&#39;test.shp&#39;</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="n">DriverName</span><span class="p">)</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">FileName</span><span class="p">):</span>
     <span class="n">driver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">FileName</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="is-ogr-installed">
<h2>Is Ogr Installed<a class="headerlink" href="#is-ogr-installed" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
  <span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
  <span class="k">print</span> <span class="s">&#39;Import of ogr from osgeo worked.  Hurray!</span><span class="se">\n</span><span class="s">&#39;</span>
<span class="k">except</span><span class="p">:</span>
  <span class="k">print</span> <span class="s">&#39;Import of ogr from osgeo failed</span><span class="se">\n\n</span><span class="s">&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="view-auto-generated-ogr-help">
<h2>View Auto Generated Ogr Help<a class="headerlink" href="#view-auto-generated-ogr-help" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code simply prints out the auto-generated help on the imported module.  In this case it&#8217;s OGR. This might not
be the best way to scan the API&#8217;s class methods. For a detailed description of the whole Python GDAL/OGR API, see the useful <a class="reference external" href="http://gdal.org/python/">API docs</a>.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">osgeo.ogr</span>
<span class="k">print</span> <span class="n">help</span><span class="p">(</span><span class="n">osgeo</span><span class="o">.</span><span class="n">ogr</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-list-of-ogr-drivers-alphabetically-a-z">
<h2>Get List of Ogr Drivers Alphabetically (A- Z)<a class="headerlink" href="#get-list-of-ogr-drivers-alphabetically-a-z" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>It&#8217;s always driven me a little nuts that the command line ogr2ogr &#8211;formats returns a &#8216;random&#8217; list of drivers.  This code returns the list of OGR drivers alphabetically from A - Z.  .</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span>
<span class="n">cnt</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverCount</span><span class="p">()</span>
<span class="n">formatsList</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c"># Empty List</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">cnt</span><span class="p">):</span>
    <span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriver</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">driverName</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">driverName</span> <span class="ow">in</span> <span class="n">formatsList</span><span class="p">:</span>
        <span class="n">formatsList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">driverName</span><span class="p">)</span>

<span class="n">formatsList</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span> <span class="c"># Sorting the messy list of ogr drivers</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">formatsList</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">i</span>
</pre></div>
</div>
</div>
<div class="section" id="is-ogr-driver-available-by-driver-name">
<h2>Is Ogr Driver Available by Driver Name<a class="headerlink" href="#is-ogr-driver-available-by-driver-name" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div><p>This code shows if a particular OGR driver is available.  The exact names are the ones used on the OGR Vector Formats page in the &#8220;Code&#8221; column  ([<a class="reference external" href="http://www.gdal.org/ogr/ogr_formats.html">formats website</a>]).  This is the same names returned when you enter <code class="docutils literal"><span class="pre">ogrinfo</span> <span class="pre">--formats</span></code> on the command line.</p>
<p>Code Example Source: [<a class="reference external" href="http://www.gdal.org/ogr/ogr_apitut.html">website</a>]</p>
</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c">## Shapefile available?</span>
<span class="n">driverName</span> <span class="o">=</span> <span class="s">&quot;ESRI Shapefile&quot;</span>
<span class="n">drv</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span> <span class="n">driverName</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">drv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver not available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span>  <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver IS available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>

<span class="c">## PostgreSQL available?</span>
<span class="n">driverName</span> <span class="o">=</span> <span class="s">&quot;PostgreSQL&quot;</span>
<span class="n">drv</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span> <span class="n">driverName</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">drv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver not available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span>  <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver IS available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>

<span class="c">## Is File GeoDatabase available?</span>
<span class="n">driverName</span> <span class="o">=</span> <span class="s">&quot;FileGDB&quot;</span>
<span class="n">drv</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span> <span class="n">driverName</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">drv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver not available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span>  <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver IS available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>

<span class="c">## SDE available?</span>
<span class="n">driverName</span> <span class="o">=</span> <span class="s">&quot;SDE&quot;</span>
<span class="n">drv</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span> <span class="n">driverName</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">drv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver not available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span>  <span class="s">&quot;</span><span class="si">%s</span><span class="s"> driver IS available.</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">driverName</span>
</pre></div>
</div>
</div>
<div class="section" id="force-ogr-use-named-driver">
<h2>Force Ogr Use Named Driver<a class="headerlink" href="#force-ogr-use-named-driver" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div><p>Use only the specified driver to attempt to read the data file, taking into account special nature of the CSV driver which normally requires a .csv extension.</p>
<p>Source, Luke Pinner on GIS Stack Exchange: [<a class="reference external" href="http://gis.stackexchange.com/questions/141905/force-ogr-to-use-specific-driver-for-input-format">website</a>]</p>
</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">in_file</span><span class="p">,</span> <span class="n">in_format</span><span class="p">,</span> <span class="n">out_file</span><span class="p">,</span> <span class="n">out_format</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">in_format</span> <span class="o">==</span> <span class="s">&#39;CSV&#39;</span> <span class="ow">and</span> <span class="n">in_file</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s">&#39;csv&#39;</span><span class="p">:</span>
                <span class="n">in_file</span> <span class="o">=</span> <span class="s">&#39;CSV:&#39;</span> <span class="o">+</span> <span class="n">in_file</span>
        <span class="n">in_ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="n">in_format</span><span class="p">)</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">in_file</span><span class="p">)</span>
        <span class="n">out_ds</span>  <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="n">out_format</span><span class="p">)</span><span class="o">.</span><span class="n">CopyDataSource</span><span class="p">(</span><span class="n">in_ds</span><span class="p">,</span> <span class="n">out_file</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
        <span class="n">main</span><span class="p">(</span><span class="o">*</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
</pre></div>
</div>
<p>Usage:</p>
<blockquote>
<div><p>python ogr-convert.py [in file] [format driver] [out file/dir] {out format}</p>
<p>python ogr-convert.py x:incomingcoolstuff.txt CSV d:shapefiles</p>
</div></blockquote>
</div>
<div class="section" id="get-shapefile-feature-count">
<h2>Get Shapefile Feature Count<a class="headerlink" href="#get-shapefile-feature-count" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example opens a shapefile and returns the number of features in it.  Solution mined from: [<a class="reference external" href="http://www.gis.usu.edu/~chrisg/python/2009/lectures/ospy_slides1.pdf">web site</a>]</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">daShapefile</span> <span class="o">=</span> <span class="s">r&quot;C:\Temp\Voting_Centers_and_Ballot_Sites.shp&quot;</span>

<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>

<span class="n">dataSource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">daShapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="c"># 0 means read-only. 1 means writeable.</span>

<span class="c"># Check to see if shapefile is found.</span>
<span class="k">if</span> <span class="n">dataSource</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Could not open </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">daShapefile</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&#39;Opened </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">daShapefile</span><span class="p">)</span>
    <span class="n">layer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
    <span class="n">featureCount</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetFeatureCount</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&quot;Number of features in </span><span class="si">%s</span><span class="s">: </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">daShapefile</span><span class="p">),</span><span class="n">featureCount</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-all-postgis-layers-in-a-postgresql-database">
<h2>Get All PostGIS layers in a PostgreSQL Database<a class="headerlink" href="#get-all-postgis-layers-in-a-postgresql-database" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This returns all the layers in a database of your choosing sorted in alphabetical order (of course).  Just fill in the missing information and it should work.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">databaseServer</span> <span class="o">=</span> <span class="s">&quot;&lt;IP of database server OR Name of database server&quot;</span>
<span class="n">databaseName</span> <span class="o">=</span> <span class="s">&quot;&lt;Name of database&gt;&quot;</span>
<span class="n">databaseUser</span> <span class="o">=</span> <span class="s">&quot;&lt;User name&gt;&quot;</span>
<span class="n">databasePW</span> <span class="o">=</span> <span class="s">&quot;&lt;User password&gt;&quot;</span>


<span class="n">connString</span> <span class="o">=</span> <span class="s">&quot;PG: host=</span><span class="si">%s</span><span class="s"> dbname=</span><span class="si">%s</span><span class="s"> user=</span><span class="si">%s</span><span class="s"> password=</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">databaseServer</span><span class="p">,</span><span class="n">databaseName</span><span class="p">,</span><span class="n">databaseUser</span><span class="p">,</span><span class="n">databasePW</span><span class="p">)</span>

<span class="n">conn</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">connString</span><span class="p">)</span>

<span class="n">layerList</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">conn</span><span class="p">:</span>
    <span class="n">daLayer</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">daLayer</span> <span class="ow">in</span> <span class="n">layerList</span><span class="p">:</span>
        <span class="n">layerList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">daLayer</span><span class="p">)</span>

<span class="n">layerList</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">layerList</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">j</span>

<span class="c"># Close connection</span>
<span class="n">conn</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="get-postgis-layer-feature-count-by-layer-name">
<h2>Get PostGIS Layer Feature Count By Layer Name<a class="headerlink" href="#get-postgis-layer-feature-count-by-layer-name" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example opens a postgis connection and gets the specified layer name if it exists in the database. Otherwise it throws a nice error message</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">databaseServer</span> <span class="o">=</span> <span class="s">&quot;&lt;IP of database server OR Name of database server&quot;</span>
<span class="n">databaseName</span> <span class="o">=</span> <span class="s">&quot;&lt;Name of database&gt;&quot;</span>
<span class="n">databaseUser</span> <span class="o">=</span> <span class="s">&quot;&lt;User name&gt;&quot;</span>
<span class="n">databasePW</span> <span class="o">=</span> <span class="s">&quot;&lt;User password&gt;&quot;</span>
<span class="n">connString</span> <span class="o">=</span> <span class="s">&quot;PG: host=</span><span class="si">%s</span><span class="s"> dbname=</span><span class="si">%s</span><span class="s"> user=</span><span class="si">%s</span><span class="s"> password=</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">databaseServer</span><span class="p">,</span><span class="n">databaseName</span><span class="p">,</span><span class="n">databaseUser</span><span class="p">,</span><span class="n">databasePW</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">GetPGLayer</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">):</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">connString</span><span class="p">)</span>

    <span class="n">lyr</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
    <span class="k">if</span> <span class="n">lyr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: layer name = &quot;</span><span class="si">%s</span><span class="s">&quot; could not be found in database &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">lyr_name</span><span class="p">,</span> <span class="n">databaseName</span> <span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">featureCount</span> <span class="o">=</span> <span class="n">lyr</span><span class="o">.</span><span class="n">GetFeatureCount</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&quot;Number of features in </span><span class="si">%s</span><span class="s">: </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">lyr_name</span> <span class="p">,</span> <span class="n">featureCount</span> <span class="p">)</span>

    <span class="c"># Close connection</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="bp">None</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: you must pass at least one argument -- the layer name argument&#39;</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">lyr_name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">GetPGLayer</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-all-layers-in-an-esri-file-geodatabase">
<h2>Get all layers in an Esri File GeoDataBase<a class="headerlink" href="#get-all-layers-in-an-esri-file-geodatabase" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This returns all the layers in a Esri FileGDB in alphabetical order (of course). It needs GDAL/OGR 1.11.0 + but not any Esri dependency.
That&#8217;s the benefit of the <a class="reference external" href="http://www.gdal.org/drv_openfilegdb.html">OpenFileGDB driver developed by Ewen Rouault</a> relative to the <a class="reference external" href="http://www.gdal.org/drv_filegdb.html">FileGDB driver</a>.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># standard imports</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="c"># import OGR</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># use OGR specific exceptions</span>
<span class="n">ogr</span><span class="o">.</span><span class="n">UseExceptions</span><span class="p">()</span>

<span class="c"># get the driver</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;OpenFileGDB&quot;</span><span class="p">)</span>

<span class="c"># opening the FileGDB</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">gdb</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">gdb_path</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">e</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>

<span class="c"># list to store layers&#39;names</span>
<span class="n">featsClassList</span> <span class="o">=</span> <span class="p">[]</span>

<span class="c"># parsing layers by index</span>
<span class="k">for</span> <span class="n">featsClass_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">gdb</span><span class="o">.</span><span class="n">GetLayerCount</span><span class="p">()):</span>
    <span class="n">featsClass</span> <span class="o">=</span> <span class="n">gdb</span><span class="o">.</span><span class="n">GetLayerByIndex</span><span class="p">(</span><span class="n">featsClass_idx</span><span class="p">)</span>
    <span class="n">featsClassList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">featsClass</span><span class="o">.</span><span class="n">GetName</span><span class="p">())</span>

<span class="c"># sorting</span>
<span class="n">featsClassList</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

<span class="c"># printing</span>
<span class="k">for</span> <span class="n">featsClass</span> <span class="ow">in</span> <span class="n">featsClassList</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">featsClass</span>

<span class="c"># clean close</span>
<span class="k">del</span> <span class="n">gdb</span>
</pre></div>
</div>
</div>
<div class="section" id="load-data-to-memory">
<h2>Load data to memory<a class="headerlink" href="#load-data-to-memory" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This illustrates how to copy a dataset to memory with write access, providing fast data access.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c">#open an input datasource</span>
<span class="n">indriver</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;SQLite&#39;</span><span class="p">)</span>
<span class="n">srcdb</span> <span class="o">=</span> <span class="n">indriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;OUTDATA.sqlite&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>

<span class="c">#create an output datasource in memory</span>
<span class="n">outdriver</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;MEMORY&#39;</span><span class="p">)</span>
<span class="n">source</span><span class="o">=</span><span class="n">outdriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="s">&#39;memData&#39;</span><span class="p">)</span>

<span class="c">#open the memory datasource with write access</span>
<span class="n">tmp</span><span class="o">=</span><span class="n">outdriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;memData&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>

<span class="c">#copy a layer to memory</span>
<span class="n">pipes_mem</span><span class="o">=</span><span class="n">source</span><span class="o">.</span><span class="n">CopyLayer</span><span class="p">(</span><span class="n">srcdb</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="s">&#39;pipes&#39;</span><span class="p">),</span><span class="s">&#39;pipes&#39;</span><span class="p">,[</span><span class="s">&#39;OVERWRITE=YES&#39;</span><span class="p">])</span>

<span class="c">#the new layer can be directly accessed via the handle pipes_mem or as source.GetLayer(&#39;pipes&#39;):</span>
<span class="n">layer</span><span class="o">=</span><span class="n">source</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="s">&#39;pipes&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&#39;SOMETHING&#39;</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="iterate-over-features">
<h2>Iterate over Features<a class="headerlink" href="#iterate-over-features" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">shapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">dataSource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">shapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">&quot;STATE_NAME&quot;</span><span class="p">)</span>
<span class="n">layer</span><span class="o">.</span><span class="n">ResetReading</span><span class="p">()</span>
</pre></div>
</div>
<p>You must call <a class="reference external" href="http://gdal.org/python/osgeo.ogr.Layer-class.html#ResetReading">ResetReading</a> if you want to start iterating over the layer again.</p>
</div>
<div class="section" id="get-geometry-from-each-feature-in-a-layer">
<h2>Get Geometry from each Feature in a Layer<a class="headerlink" href="#get-geometry-from-each-feature-in-a-layer" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">shapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">dataSource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">shapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">geom</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="filter-by-attribute">
<h2>Filter by attribute<a class="headerlink" href="#filter-by-attribute" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">shapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">dataSource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">shapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="n">layer</span><span class="o">.</span><span class="n">SetAttributeFilter</span><span class="p">(</span><span class="s">&quot;SUB_REGION = &#39;Pacific&#39;&quot;</span><span class="p">)</span>

<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">&quot;STATE_NAME&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="spatial-filter">
<h2>Spatial Filter<a class="headerlink" href="#spatial-filter" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">shapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">dataSource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">shapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((-103.81402655265633 50.253951270672125,-102.94583419409656 51.535568561879401,-100.34125711841725 51.328856095555651,-100.34125711841725 51.328856095555651,-93.437060743203844 50.460663736995883,-93.767800689321859 46.450441890315041,-94.635993047881612 41.613370178339181,-100.75468205106476 41.365315218750681,-106.12920617548238 42.564247523428456,-105.96383620242338 47.277291755610058,-103.81402655265633 50.253951270672125))&quot;</span>
<span class="n">layer</span><span class="o">.</span><span class="n">SetSpatialFilter</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">))</span>

<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">&quot;STATE_NAME&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-shapefile-fields-get-the-user-defined-fields">
<h2>Get Shapefile Fields - Get the user defined fields<a class="headerlink" href="#get-shapefile-fields-get-the-user-defined-fields" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example returns the field names of the user defined (created) fields.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">daShapefile</span> <span class="o">=</span> <span class="s">r&quot;C:\Temp\Voting_Centers_and_Ballot_Sites.shp&quot;</span>

<span class="n">dataSource</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">daShapefile</span><span class="p">)</span>
<span class="n">daLayer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">layerDefinition</span> <span class="o">=</span> <span class="n">daLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>


<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
    <span class="k">print</span> <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="get-shapefile-fields-and-types-get-the-user-defined-fields">
<h2>Get Shapefile Fields and Types - Get the user defined fields<a class="headerlink" href="#get-shapefile-fields-and-types-get-the-user-defined-fields" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example returns the field names of the user defined (created) fields and the data types they are.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">daShapefile</span> <span class="o">=</span> <span class="s">r&quot;C:\Temp\iDay\CWI_Wetlands.shp&quot;</span>

<span class="n">dataSource</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">daShapefile</span><span class="p">)</span>
<span class="n">daLayer</span> <span class="o">=</span> <span class="n">dataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">layerDefinition</span> <span class="o">=</span> <span class="n">daLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>


<span class="k">print</span> <span class="s">&quot;Name  -  Type  Width  Precision&quot;</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
    <span class="n">fieldName</span> <span class="o">=</span>  <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
    <span class="n">fieldTypeCode</span> <span class="o">=</span> <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetType</span><span class="p">()</span>
    <span class="n">fieldType</span> <span class="o">=</span> <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetFieldTypeName</span><span class="p">(</span><span class="n">fieldTypeCode</span><span class="p">)</span>
    <span class="n">fieldWidth</span> <span class="o">=</span> <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetWidth</span><span class="p">()</span>
    <span class="n">GetPrecision</span> <span class="o">=</span> <span class="n">layerDefinition</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetPrecision</span><span class="p">()</span>

    <span class="k">print</span> <span class="n">fieldName</span> <span class="o">+</span> <span class="s">&quot; - &quot;</span> <span class="o">+</span> <span class="n">fieldType</span><span class="o">+</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">fieldWidth</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">GetPrecision</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-postgis-layer-fields-get-the-user-defined-fields">
<h2>Get PostGIS Layer Fields - Get the user defined fields<a class="headerlink" href="#get-postgis-layer-fields-get-the-user-defined-fields" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example returns the field names of the user defined (created) fields.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">databaseServer</span> <span class="o">=</span> <span class="s">&quot;&lt;IP of database server OR Name of database server&quot;</span>
<span class="n">databaseName</span> <span class="o">=</span> <span class="s">&quot;&lt;Name of database&gt;&quot;</span>
<span class="n">databaseUser</span> <span class="o">=</span> <span class="s">&quot;&lt;User name&gt;&quot;</span>
<span class="n">databasePW</span> <span class="o">=</span> <span class="s">&quot;&lt;User password&gt;&quot;</span>
<span class="n">connString</span> <span class="o">=</span> <span class="s">&quot;PG: host=</span><span class="si">%s</span><span class="s"> dbname=</span><span class="si">%s</span><span class="s"> user=</span><span class="si">%s</span><span class="s"> password=</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">databaseServer</span><span class="p">,</span><span class="n">databaseName</span><span class="p">,</span><span class="n">databaseUser</span><span class="p">,</span><span class="n">databasePW</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">GetPGLayerFields</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">):</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">connString</span><span class="p">)</span>

    <span class="n">lyr</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
    <span class="k">if</span> <span class="n">lyr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: layer name = &quot;</span><span class="si">%s</span><span class="s">&quot; could not be found in database &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">lyr_name</span><span class="p">,</span> <span class="n">databaseName</span> <span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">lyrDefn</span> <span class="o">=</span> <span class="n">lyr</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>


    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()</span> <span class="p">):</span>
        <span class="k">print</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span> <span class="n">i</span> <span class="p">)</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>

    <span class="c"># Close connection</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="bp">None</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: you must pass at least one argument -- the layer name argument&#39;</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">lyr_name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">GetPGLayerFields</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-postgis-layer-fields-and-types-get-the-user-defined-fields">
<h2>Get PostGIS Layer Fields and Types - Get the user defined fields<a class="headerlink" href="#get-postgis-layer-fields-and-types-get-the-user-defined-fields" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This code example returns the field names of the user defined (created) fields and the data types they are.</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">sys</span>


<span class="n">databaseServer</span> <span class="o">=</span> <span class="s">&quot;&lt;IP of database server OR Name of database server&quot;</span>
<span class="n">databaseName</span> <span class="o">=</span> <span class="s">&quot;&lt;Name of database&gt;&quot;</span>
<span class="n">databaseUser</span> <span class="o">=</span> <span class="s">&quot;&lt;User name&gt;&quot;</span>
<span class="n">databasePW</span> <span class="o">=</span> <span class="s">&quot;&lt;User password&gt;&quot;</span>
<span class="n">connString</span> <span class="o">=</span> <span class="s">&quot;PG: host=</span><span class="si">%s</span><span class="s"> dbname=</span><span class="si">%s</span><span class="s"> user=</span><span class="si">%s</span><span class="s"> password=</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">databaseServer</span><span class="p">,</span><span class="n">databaseName</span><span class="p">,</span><span class="n">databaseUser</span><span class="p">,</span><span class="n">databasePW</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">GetPGLayerFieldTypes</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">):</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">connString</span><span class="p">)</span>

    <span class="n">lyr</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
    <span class="k">if</span> <span class="n">lyr</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: layer name = &quot;</span><span class="si">%s</span><span class="s">&quot; could not be found in database &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span> <span class="n">lyr_name</span><span class="p">,</span> <span class="n">databaseName</span> <span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">lyrDefn</span> <span class="o">=</span> <span class="n">lyr</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()</span> <span class="p">):</span>
        <span class="n">fieldName</span> <span class="o">=</span>  <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
        <span class="n">fieldTypeCode</span> <span class="o">=</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetType</span><span class="p">()</span>
        <span class="n">fieldType</span> <span class="o">=</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetFieldTypeName</span><span class="p">(</span><span class="n">fieldTypeCode</span><span class="p">)</span>
        <span class="n">fieldWidth</span> <span class="o">=</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetWidth</span><span class="p">()</span>
        <span class="n">GetPrecision</span> <span class="o">=</span> <span class="n">lyrDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetPrecision</span><span class="p">()</span>

        <span class="k">print</span> <span class="n">fieldName</span> <span class="o">+</span> <span class="s">&quot; - &quot;</span> <span class="o">+</span> <span class="n">fieldType</span><span class="o">+</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">fieldWidth</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">GetPrecision</span><span class="p">)</span>

    <span class="c"># Close connection</span>
    <span class="n">conn</span> <span class="o">=</span> <span class="bp">None</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span> <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#39;[ ERROR ]: you must pass at least one argument -- the layer name argument&#39;</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">lyr_name</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">GetPGLayerFieldTypes</span><span class="p">(</span> <span class="n">lyr_name</span> <span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="get-a-layer-s-capabilities">
<h2>Get a Layer&#8217;s Capabilities<a class="headerlink" href="#get-a-layer-s-capabilities" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&quot;states.shp&quot;</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">capabilities</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCRandomRead</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCSequentialWrite</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCRandomWrite</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCFastSpatialFilter</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCFastFeatureCount</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCFastGetExtent</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCCreateField</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCDeleteField</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCReorderFields</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCAlterFieldDefn</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCTransactions</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCDeleteFeature</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCFastSetNextByIndex</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCStringsAsUTF8</span><span class="p">,</span>
    <span class="n">ogr</span><span class="o">.</span><span class="n">OLCIgnoreFields</span>
<span class="p">]</span>

<span class="k">print</span><span class="p">(</span><span class="s">&quot;Layer Capabilities:&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">cap</span> <span class="ow">in</span> <span class="n">capabilities</span><span class="p">:</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;  </span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">cap</span><span class="p">,</span> <span class="n">layer</span><span class="o">.</span><span class="n">TestCapability</span><span class="p">(</span><span class="n">cap</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="get-wfs-layers-and-iterate-over-features">
<h2>Get WFS layers and iterate over features<a class="headerlink" href="#get-wfs-layers-and-iterate-over-features" title="Permalink to this headline">¶</a></h2>
<p>This recipe queries a WFS services and fetches features from a large layer. It sets up the GDAL
configuration to using WFS paging if it is supported.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span><span class="p">,</span> <span class="n">gdal</span>
<span class="k">except</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">&#39;ERROR: cannot find GDAL/OGR modules&#39;</span><span class="p">)</span>

<span class="c"># Set the driver (optional)</span>
<span class="n">wfs_drv</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;WFS&#39;</span><span class="p">)</span>

<span class="c"># Speeds up querying WFS capabilities for services with alot of layers</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;OGR_WFS_LOAD_MULTIPLE_LAYER_DEFN&#39;</span><span class="p">,</span> <span class="s">&#39;NO&#39;</span><span class="p">)</span>

<span class="c"># Set config for paging. Works on WFS 2.0 services and WFS 1.0 and 1.1 with some other services.</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;OGR_WFS_PAGING_ALLOWED&#39;</span><span class="p">,</span> <span class="s">&#39;YES&#39;</span><span class="p">)</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;OGR_WFS_PAGE_SIZE&#39;</span><span class="p">,</span> <span class="s">&#39;10000&#39;</span><span class="p">)</span>

<span class="c"># Open the webservice</span>
<span class="n">url</span> <span class="o">=</span> <span class="s">&#39;http://example-service.com/wfs&#39;</span>
<span class="n">wfs_ds</span> <span class="o">=</span> <span class="n">wfs_drv</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;WFS:&#39;</span> <span class="o">+</span> <span class="n">url</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">wfs_ds</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">&#39;ERROR: can not open WFS datasource&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">pass</span>

<span class="c"># iterate over available layers</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">wfs_ds</span><span class="o">.</span><span class="n">GetLayerCount</span><span class="p">()):</span>
    <span class="n">layer</span> <span class="o">=</span> <span class="n">wfs_ds</span><span class="o">.</span><span class="n">GetLayerByIndex</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">srs</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetSpatialRef</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&#39;Layer: </span><span class="si">%s</span><span class="s">, Features: </span><span class="si">%s</span><span class="s">, SR: </span><span class="si">%s</span><span class="s">...&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">GetName</span><span class="p">(),</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetFeatureCount</span><span class="p">(),</span> <span class="n">srs</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">50</span><span class="p">])</span>

    <span class="c"># iterate over features</span>
    <span class="n">feat</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetNextFeature</span><span class="p">()</span>
    <span class="k">while</span> <span class="n">feat</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">feat</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetNextFeature</span><span class="p">()</span>
        <span class="c"># do something more..</span>
    <span class="n">feat</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Get a specific layer</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">wfs_ds</span><span class="o">.</span><span class="n">GetLayerByName</span><span class="p">(</span><span class="s">&quot;largelayer&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">layer</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">&#39;ERROR: can not find layer in service&#39;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">pass</span>
</pre></div>
</div>
</div>
<div class="section" id="set-http-proxy-options-before-fetching-a-web-datasource">
<h2>Set HTTP Proxy options before fetching a web datasource<a class="headerlink" href="#set-http-proxy-options-before-fetching-a-web-datasource" title="Permalink to this headline">¶</a></h2>
<p>This recipe sets options for a HTTP proxy service that using NTLM authentication (typical
for corporate environments that using Active directory for single sign-on proxy support).
More information about the GDAL HTTP proxy options can be found <a class="reference external" href="http://trac.osgeo.org/gdal/wiki/ConfigOptions#GDALOGRHTTPoptions">here</a></p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span><span class="p">,</span> <span class="n">osr</span><span class="p">,</span> <span class="n">gdal</span>
<span class="k">except</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">&#39;ERROR: cannot find GDAL/OGR modules&#39;</span><span class="p">)</span>

<span class="n">server</span> <span class="o">=</span> <span class="s">&#39;proxy.example.com&#39;</span>
<span class="n">port</span> <span class="o">=</span> <span class="mi">3128</span>

<span class="c"># specify proxy server</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;GDAL_HTTP_PROXY&#39;</span><span class="p">,</span> <span class="n">server</span> <span class="o">+</span> <span class="s">&#39;:&#39;</span> <span class="o">+</span> <span class="n">port</span><span class="p">)</span>

<span class="c"># setup proxy authentication option for NTLM with no username or password so single sign-on works</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;GDAL_PROXY_AUTH&#39;</span><span class="p">,</span> <span class="s">&#39;NTLM&#39;</span><span class="p">)</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">SetConfigOption</span><span class="p">(</span><span class="s">&#39;GDAL_HTTP_PROXYUSERPWD&#39;</span><span class="p">,</span> <span class="s">&#39; : &#39;</span><span class="p">)</span>

<span class="c"># now fetch a HTTP datasource and do something...</span>
<span class="n">ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;http://featureserver/cities/.geojson&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">ds</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">&#39;ERROR: can not open GeoJSON datasource&#39;</span><span class="p">)</span>
<span class="n">lyr</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="s">&#39;OGRGeoJSON&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">lyr</span><span class="p">:</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">feat</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">geom</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="read-a-csv-of-coordinates-as-an-ogrvrtlayer">
<h2>Read a CSV of Coordinates as an OGRVRTLayer<a class="headerlink" href="#read-a-csv-of-coordinates-as-an-ogrvrtlayer" title="Permalink to this headline">¶</a></h2>
<p>GDAL/OGR has a <a class="reference external" href="http://www.gdal.org/ogr/drv_vrt.html">Virtual Format spec</a> that allows you to derive layers from flat tables such as a CSV &#8211; it does a lot more than that too so go read about it. In the example below we are reading in a CSV with X,Y columns and values. That CSV file is wrapped by an XML file that describes it as an OGR layer. Below are all the necessary pieces and a script that reads the XML file and prints out point geometries.</p>
<p>Our CSV file named <cite>example.csv</cite> looks like this:</p>
<div class="highlight-bash"><div class="highlight"><pre>ID,X,Y
1,-127.234343,47.234325
2,-127.003243,46.234343
3,-127.345646,45.234324
4,-126.234324,44.324234
</pre></div>
</div>
<p>Our OGRVRTLayer XML file called <cite>example_wrapper.vrt</cite> looks like this:</p>
<div class="highlight-bash"><div class="highlight"><pre>&lt;OGRVRTDataSource&gt;
    &lt;OGRVRTLayer <span class="nv">name</span><span class="o">=</span><span class="s2">&quot;example&quot;</span>&gt;
        &lt;SrcDataSource&gt;example.csv&lt;/SrcDataSource&gt;
        &lt;SrcLayer&gt;example&lt;/SrcLayer&gt;
        &lt;GeometryType&gt;wkbPoint&lt;/GeometryType&gt;
            &lt;LayerSRS&gt;WGS84&lt;/LayerSRS&gt;
        &lt;GeometryField <span class="nv">encoding</span><span class="o">=</span><span class="s2">&quot;PointFromColumns&quot;</span> <span class="nv">x</span><span class="o">=</span><span class="s2">&quot;X&quot;</span> <span class="nv">y</span><span class="o">=</span><span class="s2">&quot;Y&quot;</span>/&gt;
    &lt;/OGRVRTLayer&gt;
&lt;/OGRVRTDataSource&gt;
</pre></div>
</div>
<p>Now let&#8217;s print out the point geometries:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="n">ogr</span><span class="o">.</span><span class="n">UseExceptions</span><span class="p">()</span>

<span class="n">inDataSource</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&quot;example_wrapper.vrt&quot;</span><span class="p">)</span>
<span class="n">lyr</span> <span class="o">=</span> <span class="n">inDataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="s">&#39;example&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">lyr</span><span class="p">:</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">feat</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
    <span class="k">print</span> <span class="n">geom</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-new-layer-from-the-extent-of-an-existing-layer">
<h2>Create a new Layer from the extent of an existing Layer<a class="headerlink" href="#create-a-new-layer-from-the-extent-of-an-existing-layer" title="Permalink to this headline">¶</a></h2>
<img alt="_images/layer_extent.png" src="_images/layer_extent.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="c"># Get a Layer&#39;s Extent</span>
<span class="n">inShapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">inDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="n">inDriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">inShapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">inLayer</span> <span class="o">=</span> <span class="n">inDataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">extent</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetExtent</span><span class="p">()</span>

<span class="c"># Create a Polygon from the extent tuple</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">extent</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">extent</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">extent</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">extent</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">extent</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">extent</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="c"># Save extent to a new Shapefile</span>
<span class="n">outShapefile</span> <span class="o">=</span> <span class="s">&quot;states_extent.shp&quot;</span>
<span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>

<span class="c"># Remove output shapefile if it already exists</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">):</span>
    <span class="n">outDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>

<span class="c"># Create the output shapefile</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&quot;states_extent&quot;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>

<span class="c"># Add an ID field</span>
<span class="n">idField</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;id&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTInteger</span><span class="p">)</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">idField</span><span class="p">)</span>

<span class="c"># Create the feature and set values</span>
<span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="n">feature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
<span class="n">feature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
<span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;id&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">feature</span><span class="p">)</span>
<span class="n">feature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Save and close DataSource</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="save-the-convex-hull-of-all-geometry-from-an-input-layer-to-an-output-layer">
<h2>Save the convex hull of all geometry from an input Layer to an output Layer<a class="headerlink" href="#save-the-convex-hull-of-all-geometry-from-an-input-layer-to-an-output-layer" title="Permalink to this headline">¶</a></h2>
<img alt="_images/layer_convexhull.png" src="_images/layer_convexhull.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="c"># Get a Layer</span>
<span class="n">inShapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">inDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="n">inDriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">inShapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">inLayer</span> <span class="o">=</span> <span class="n">inDataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="c"># Collect all Geometry</span>
<span class="n">geomcol</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbGeometryCollection</span><span class="p">)</span>
<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">inLayer</span><span class="p">:</span>
    <span class="n">geomcol</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">feature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">())</span>

<span class="c"># Calculate convex hull</span>
<span class="n">convexhull</span> <span class="o">=</span> <span class="n">geomcol</span><span class="o">.</span><span class="n">ConvexHull</span><span class="p">()</span>

<span class="c"># Save extent to a new Shapefile</span>
<span class="n">outShapefile</span> <span class="o">=</span> <span class="s">&quot;states_convexhull.shp&quot;</span>
<span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>

<span class="c"># Remove output shapefile if it already exists</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">):</span>
    <span class="n">outDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>

<span class="c"># Create the output shapefile</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&quot;states_convexhull&quot;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>

<span class="c"># Add an ID field</span>
<span class="n">idField</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;id&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTInteger</span><span class="p">)</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">idField</span><span class="p">)</span>

<span class="c"># Create the feature and set values</span>
<span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="n">feature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
<span class="n">feature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">convexhull</span><span class="p">)</span>
<span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;id&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">feature</span><span class="p">)</span>
<span class="n">feature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Save and close DataSource</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="save-centroids-of-input-layer-to-an-output-layer">
<h2>Save centroids of input Layer to an output Layer<a class="headerlink" href="#save-centroids-of-input-layer-to-an-output-layer" title="Permalink to this headline">¶</a></h2>
<p>Inspired by: <a class="reference external" href="http://www.kralidis.ca/blog/2010/04/28/batch-centroid-calculations-with-python-and-ogr/">http://www.kralidis.ca/blog/2010/04/28/batch-centroid-calculations-with-python-and-ogr/</a></p>
<img alt="_images/layer_centroids.png" src="_images/layer_centroids.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="c"># Get the input Layer</span>
<span class="n">inShapefile</span> <span class="o">=</span> <span class="s">&quot;states.shp&quot;</span>
<span class="n">inDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="n">inDriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">inShapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">inLayer</span> <span class="o">=</span> <span class="n">inDataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

<span class="c"># Create the output Layer</span>
<span class="n">outShapefile</span> <span class="o">=</span> <span class="s">&quot;states_centroids.shp&quot;</span>
<span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>

<span class="c"># Remove output shapefile if it already exists</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">):</span>
    <span class="n">outDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>

<span class="c"># Create the output shapefile</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&quot;states_centroids&quot;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>

<span class="c"># Add input Layer Fields to the output Layer</span>
<span class="n">inLayerDefn</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
    <span class="n">fieldDefn</span> <span class="o">=</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">fieldDefn</span><span class="p">)</span>

<span class="c"># Get the output Layer&#39;s Feature Definition</span>
<span class="n">outLayerDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

<span class="c"># Add features to the ouput Layer</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetFeatureCount</span><span class="p">()):</span>
    <span class="c"># Get the input Feature</span>
    <span class="n">inFeature</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetFeature</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="c"># Create output Feature</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="p">)</span>
    <span class="c"># Add field values from input Layer</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
        <span class="n">outFeature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetNameRef</span><span class="p">(),</span> <span class="n">inFeature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
    <span class="c"># Set geometry as centroid</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">inFeature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
    <span class="n">inFeature</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">centroid</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span>
    <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">centroid</span><span class="p">)</span>
    <span class="c"># Add new feature to output Layer</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Save and close DataSources</span>
<span class="n">inDataSource</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-new-shapefile-and-add-data">
<h2>Create a New Shapefile and Add Data<a class="headerlink" href="#create-a-new-shapefile-and-add-data" title="Permalink to this headline">¶</a></h2>
<p>This recipe parses a delimited text file of volcano location data and creates a shapefile.
The CSV file <code class="docutils literal"><span class="pre">volcano_data.txt</span></code> contains the following fields, separated by a tab character (\t):</p>
<ul class="simple">
<li>Name</li>
<li>Region</li>
<li>Latitude</li>
<li>Longitude</li>
<li>Elevation</li>
</ul>
<p><cite>Taken from The Geospatial Desktop book.</cite></p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Parse a delimited text file of volcano data and create a shapefile</span>

<span class="kn">import</span> <span class="nn">osgeo.ogr</span> <span class="kn">as</span> <span class="nn">ogr</span>
<span class="kn">import</span> <span class="nn">osgeo.osr</span> <span class="kn">as</span> <span class="nn">osr</span>

<span class="c"># use a dictionary reader so we can access by field name</span>
<span class="n">reader</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">DictReader</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&quot;volcano_data.txt&quot;</span><span class="p">,</span><span class="s">&quot;rb&quot;</span><span class="p">),</span>
    <span class="n">delimiter</span><span class="o">=</span><span class="s">&#39;</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span>
    <span class="n">quoting</span><span class="o">=</span><span class="n">csv</span><span class="o">.</span><span class="n">QUOTE_NONE</span><span class="p">)</span>

<span class="c"># set up the shapefile driver</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>

<span class="c"># create the data source</span>
<span class="n">data_source</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="s">&quot;volcanoes.shp&quot;</span><span class="p">)</span>

<span class="c"># create the spatial reference, WGS84</span>
<span class="n">srs</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">srs</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">4326</span><span class="p">)</span>

<span class="c"># create the layer</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">data_source</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&quot;volcanoes&quot;</span><span class="p">,</span> <span class="n">srs</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>

<span class="c"># Add the fields we&#39;re interested in</span>
<span class="n">field_name</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;Name&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTString</span><span class="p">)</span>
<span class="n">field_name</span><span class="o">.</span><span class="n">SetWidth</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
<span class="n">field_region</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;Region&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTString</span><span class="p">)</span>
<span class="n">field_region</span><span class="o">.</span><span class="n">SetWidth</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">field_region</span><span class="p">)</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;Latitude&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTReal</span><span class="p">))</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;Longitude&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTReal</span><span class="p">))</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="s">&quot;Elevation&quot;</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTInteger</span><span class="p">))</span>

<span class="c"># Process the text file and add the attributes and features to the shapefile</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
  <span class="c"># create the feature</span>
  <span class="n">feature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">layer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">())</span>
  <span class="c"># Set the attributes using the values from the delimited text file</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;Name&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="s">&#39;Name&#39;</span><span class="p">])</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;Region&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="s">&#39;Region&#39;</span><span class="p">])</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;Latitude&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="s">&#39;Latitude&#39;</span><span class="p">])</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;Longitude&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="s">&#39;Longitude&#39;</span><span class="p">])</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="s">&quot;Elevation&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="s">&#39;Elev&#39;</span><span class="p">])</span>

  <span class="c"># create the WKT for the feature using Python string formatting</span>
  <span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POINT(</span><span class="si">%f</span><span class="s"> </span><span class="si">%f</span><span class="s">)&quot;</span> <span class="o">%</span>  <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="s">&#39;Longitude&#39;</span><span class="p">])</span> <span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="s">&#39;Latitude&#39;</span><span class="p">]))</span>

  <span class="c"># Create the point from the Well Known Txt</span>
  <span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>

  <span class="c"># Set the feature geometry using the point</span>
  <span class="n">feature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>
  <span class="c"># Create the feature in the layer (shapefile)</span>
  <span class="n">layer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">feature</span><span class="p">)</span>
  <span class="c"># Dereference the feature</span>
  <span class="n">feature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Save and close the data source</span>
<span class="n">data_source</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-postgis-table-from-wkt">
<h2>Create a PostGIS table from WKT<a class="headerlink" href="#create-a-postgis-table-from-wkt" title="Permalink to this headline">¶</a></h2>
<p>This recipe creates a new table in an existing PostGIS database.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">osr</span>

<span class="n">database</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>
<span class="n">usr</span> <span class="o">=</span> <span class="s">&#39;postgres&#39;</span>
<span class="n">pw</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
<span class="n">table</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POINT (1120351.5712494177 741921.4223245403)&quot;</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>

<span class="n">connectionString</span> <span class="o">=</span> <span class="s">&quot;PG:dbname=&#39;</span><span class="si">%s</span><span class="s">&#39; user=&#39;</span><span class="si">%s</span><span class="s">&#39; password=&#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">database</span><span class="p">,</span><span class="n">usr</span><span class="p">,</span><span class="n">pw</span><span class="p">)</span>
<span class="n">ogrds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">connectionString</span><span class="p">)</span>

<span class="n">srs</span> <span class="o">=</span> <span class="n">osr</span><span class="o">.</span><span class="n">SpatialReference</span><span class="p">()</span>
<span class="n">srs</span><span class="o">.</span><span class="n">ImportFromEPSG</span><span class="p">(</span><span class="mi">4326</span><span class="p">)</span>

<span class="n">layer</span> <span class="o">=</span> <span class="n">ogrds</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">srs</span><span class="p">,</span> <span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;OVERWRITE=YES&#39;</span><span class="p">]</span> <span class="p">)</span>

<span class="n">layerDefn</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

<span class="n">feature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">layerDefn</span><span class="p">)</span>
<span class="n">feature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>

<span class="n">layer</span><span class="o">.</span><span class="n">StartTransaction</span><span class="p">()</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">feature</span><span class="p">)</span>
<span class="n">feature</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">layer</span><span class="o">.</span><span class="n">CommitTransaction</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="filter-and-select-input-shapefile-to-new-output-shapefile-like-ogr2ogr-cli">
<h2>Filter and Select Input Shapefile to New Output Shapefile Like ogr2ogr CLI<a class="headerlink" href="#filter-and-select-input-shapefile-to-new-output-shapefile-like-ogr2ogr-cli" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference external" href="http://www.gdal.org/ogr2ogr.html">ogr2ogr command line tool</a> is an easy way to filter, reproject and trim columns in a shapefile. The workflow below shows how we can approximate the following ogr2ogr command with the OGR api using a decently large <a class="reference external" href="http://www5.kingcounty.gov/sdc/Metadata.aspx?Layer=parcel_address">parcel shapefile from King County GIS</a> .</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># this command says read in &quot;parcel_address.shp&quot; and write out to &quot;junkmob.shp&quot;</span>
<span class="c"># where &quot;MINOR&quot; column = &#39;HYDR&#39; value and only output the &quot;PIN&quot; column</span>
<span class="c">#</span>
<span class="nv">$ </span>ogr2ogr -f <span class="s2">&quot;ESRI Shapefile&quot;</span> junkmob.shp -select pin -where <span class="s2">&quot;minor = &#39;HYDR&#39;&quot;</span> parcel_address.shp
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">(</span> <span class="n">field_name_target</span> <span class="p">):</span>
    <span class="c"># Get the input Layer</span>
    <span class="n">inShapefile</span> <span class="o">=</span> <span class="s">&quot;~/DATA/SHAPES/KC_ADMIN/parcel_address/parcel_address.shp&quot;</span>
    <span class="n">inDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
    <span class="n">inDataSource</span> <span class="o">=</span> <span class="n">inDriver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">inShapefile</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">inLayer</span> <span class="o">=</span> <span class="n">inDataSource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
    <span class="n">inLayer</span><span class="o">.</span><span class="n">SetAttributeFilter</span><span class="p">(</span><span class="s">&quot;minor = &#39;HYDR&#39;&quot;</span><span class="p">)</span>

    <span class="c"># Create the output LayerS</span>
    <span class="n">outShapefile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span> <span class="n">inShapefile</span> <span class="p">)[</span><span class="mi">0</span><span class="p">],</span> <span class="s">&quot;ogr_api_filter.shp&quot;</span> <span class="p">)</span>
    <span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>

    <span class="c"># Remove output shapefile if it already exists</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">):</span>
        <span class="n">outDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>

    <span class="c"># Create the output shapefile</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outShapefile</span><span class="p">)</span>
    <span class="n">out_lyr_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span> <span class="n">outShapefile</span> <span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span> <span class="n">out_lyr_name</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPolygon</span> <span class="p">)</span>

    <span class="c"># Add input Layer Fields to the output Layer if it is the one we want</span>
    <span class="n">inLayerDefn</span> <span class="o">=</span> <span class="n">inLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
        <span class="n">fieldDefn</span> <span class="o">=</span> <span class="n">inLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="n">fieldName</span> <span class="o">=</span> <span class="n">fieldDefn</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">fieldName</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">field_name_target</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">fieldDefn</span><span class="p">)</span>

    <span class="c"># Get the output Layer&#39;s Feature Definition</span>
    <span class="n">outLayerDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

    <span class="c"># Add features to the ouput Layer</span>
    <span class="k">for</span> <span class="n">inFeature</span> <span class="ow">in</span> <span class="n">inLayer</span><span class="p">:</span>
        <span class="c"># Create output Feature</span>
        <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="p">)</span>

        <span class="c"># Add field values from input Layer</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()):</span>
            <span class="n">fieldDefn</span> <span class="o">=</span> <span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">fieldName</span> <span class="o">=</span> <span class="n">fieldDefn</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">fieldName</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">field_name_target</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">outFeature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="n">outLayerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">.</span><span class="n">GetNameRef</span><span class="p">(),</span>
                <span class="n">inFeature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>

        <span class="c"># Set geometry as centroid</span>
        <span class="n">geom</span> <span class="o">=</span> <span class="n">inFeature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
        <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">geom</span><span class="o">.</span><span class="n">Clone</span><span class="p">())</span>
        <span class="c"># Add new feature to output Layer</span>
        <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
        <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="c"># Save and close DataSources</span>
    <span class="n">inDataSource</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;[ ERROR ]: you need to pass at least one arg -- the field_names to include in output&quot;</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">main</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="merge-ogr-layers">
<h2>Merge OGR Layers<a class="headerlink" href="#merge-ogr-layers" title="Permalink to this headline">¶</a></h2>
<p>This recipe merges OGR Layers within a directory. Files can be specfied based on with what they start and end.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">osr</span>

<span class="n">outputMergefn</span> <span class="o">=</span> <span class="s">&#39;merge.shp&#39;</span>
<span class="n">directory</span> <span class="o">=</span> <span class="s">&quot;/Users/UserName/Downloads/&quot;</span>
<span class="n">fileStartsWith</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>
<span class="n">fileEndsWith</span> <span class="o">=</span> <span class="s">&#39;.shp&#39;</span>
<span class="n">driverName</span> <span class="o">=</span> <span class="s">&#39;ESRI Shapefile&#39;</span>
<span class="n">geometryType</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span>

<span class="n">out_driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span> <span class="n">driverName</span> <span class="p">)</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outputMergefn</span><span class="p">):</span>
    <span class="n">out_driver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outputMergefn</span><span class="p">)</span>
<span class="n">out_ds</span> <span class="o">=</span> <span class="n">out_driver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outputMergefn</span><span class="p">)</span>
<span class="n">out_layer</span> <span class="o">=</span> <span class="n">out_ds</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">outputMergefn</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">geometryType</span><span class="p">)</span>

<span class="n">fileList</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span>

<span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">fileList</span><span class="p">:</span>
    <span class="k">if</span> <span class="nb">file</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">fileStartsWith</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">fileEndsWith</span><span class="p">):</span>
        <span class="k">print</span> <span class="nb">file</span>
        <span class="n">ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">directory</span><span class="o">+</span><span class="nb">file</span><span class="p">)</span>
        <span class="n">lyr</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">lyr</span><span class="p">:</span>
            <span class="n">out_feat</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">out_layer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">())</span>
            <span class="n">out_feat</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">feat</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span><span class="o">.</span><span class="n">Clone</span><span class="p">())</span>
            <span class="n">out_layer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">out_feat</span><span class="p">)</span>
            <span class="n">out_feat</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="n">out_layer</span><span class="o">.</span><span class="n">SyncToDisk</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="get-a-list-of-the-street-names-in-a-osm-file">
<h2>Get a list of the street names in a OSM file<a class="headerlink" href="#get-a-list-of-the-street-names-in-a-osm-file" title="Permalink to this headline">¶</a></h2>
<p>This recipe takes in an OSM file and prints a list of all the names of the streets in the file.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span>

<span class="n">ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;map.osm&#39;</span><span class="p">)</span>
<span class="n">layer</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c"># layer 1 for ways</span>

<span class="n">nameList</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">layer</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">&quot;highway&quot;</span><span class="p">)</span> <span class="o">!=</span> <span class="bp">None</span><span class="p">:</span>  <span class="c"># only streets</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">nameList</span><span class="p">:</span> <span class="c"># only streets that have a name and are not yet in the list</span>
            <span class="n">nameList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

<span class="k">print</span> <span class="n">nameList</span>
</pre></div>
</div>
</div>
<div class="section" id="create-fishnet-grid">
<h2>Create fishnet grid<a class="headerlink" href="#create-fishnet-grid" title="Permalink to this headline">¶</a></h2>
<p>This recipe creates a fishnet grid.</p>
<div class="highlight-shell"><div class="highlight"><pre>python grid.py grid.shp 992325.66 1484723.41 494849.32 781786.14 <span class="m">10000</span> 10000
</pre></div>
</div>
<img alt="_images/layer_fishnet.png" src="_images/layer_fishnet.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">ogr</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">ceil</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">outputGridfn</span><span class="p">,</span><span class="n">xmin</span><span class="p">,</span><span class="n">xmax</span><span class="p">,</span><span class="n">ymin</span><span class="p">,</span><span class="n">ymax</span><span class="p">,</span><span class="n">gridHeight</span><span class="p">,</span><span class="n">gridWidth</span><span class="p">):</span>

    <span class="c"># convert sys.argv to float</span>
    <span class="n">xmin</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">xmin</span><span class="p">)</span>
    <span class="n">xmax</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">xmax</span><span class="p">)</span>
    <span class="n">ymin</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">ymin</span><span class="p">)</span>
    <span class="n">ymax</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">ymax</span><span class="p">)</span>
    <span class="n">gridWidth</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">gridWidth</span><span class="p">)</span>
    <span class="n">gridHeight</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">gridHeight</span><span class="p">)</span>

    <span class="c"># get rows</span>
    <span class="n">rows</span> <span class="o">=</span> <span class="n">ceil</span><span class="p">((</span><span class="n">ymax</span><span class="o">-</span><span class="n">ymin</span><span class="p">)</span><span class="o">/</span><span class="n">gridHeight</span><span class="p">)</span>
    <span class="c"># get columns</span>
    <span class="n">cols</span> <span class="o">=</span> <span class="n">ceil</span><span class="p">((</span><span class="n">xmax</span><span class="o">-</span><span class="n">xmin</span><span class="p">)</span><span class="o">/</span><span class="n">gridWidth</span><span class="p">)</span>

    <span class="c"># start grid cell envelope</span>
    <span class="n">ringXleftOrigin</span> <span class="o">=</span> <span class="n">xmin</span>
    <span class="n">ringXrightOrigin</span> <span class="o">=</span> <span class="n">xmin</span> <span class="o">+</span> <span class="n">gridWidth</span>
    <span class="n">ringYtopOrigin</span> <span class="o">=</span> <span class="n">ymax</span>
    <span class="n">ringYbottomOrigin</span> <span class="o">=</span> <span class="n">ymax</span><span class="o">-</span><span class="n">gridHeight</span>

    <span class="c"># create output file</span>
    <span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outputGridfn</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">outputGridfn</span><span class="p">)</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outputGridfn</span><span class="p">)</span>
    <span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">outputGridfn</span><span class="p">,</span><span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span> <span class="p">)</span>
    <span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

    <span class="c"># create grid cells</span>
    <span class="n">countcols</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">countcols</span> <span class="o">&lt;</span> <span class="n">cols</span><span class="p">:</span>
        <span class="n">countcols</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="c"># reset envelope for rows</span>
        <span class="n">ringYtop</span> <span class="o">=</span> <span class="n">ringYtopOrigin</span>
        <span class="n">ringYbottom</span> <span class="o">=</span><span class="n">ringYbottomOrigin</span>
        <span class="n">countrows</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">while</span> <span class="n">countrows</span> <span class="o">&lt;</span> <span class="n">rows</span><span class="p">:</span>
            <span class="n">countrows</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
            <span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">ringXleftOrigin</span><span class="p">,</span> <span class="n">ringYtop</span><span class="p">)</span>
            <span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">ringXrightOrigin</span><span class="p">,</span> <span class="n">ringYtop</span><span class="p">)</span>
            <span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">ringXrightOrigin</span><span class="p">,</span> <span class="n">ringYbottom</span><span class="p">)</span>
            <span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">ringXleftOrigin</span><span class="p">,</span> <span class="n">ringYbottom</span><span class="p">)</span>
            <span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">ringXleftOrigin</span><span class="p">,</span> <span class="n">ringYtop</span><span class="p">)</span>
            <span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
            <span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

            <span class="c"># add new geom to layer</span>
            <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
            <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
            <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
            <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

            <span class="c"># new envelope for next poly</span>
            <span class="n">ringYtop</span> <span class="o">=</span> <span class="n">ringYtop</span> <span class="o">-</span> <span class="n">gridHeight</span>
            <span class="n">ringYbottom</span> <span class="o">=</span> <span class="n">ringYbottom</span> <span class="o">-</span> <span class="n">gridHeight</span>

        <span class="c"># new envelope for next poly</span>
        <span class="n">ringXleftOrigin</span> <span class="o">=</span> <span class="n">ringXleftOrigin</span> <span class="o">+</span> <span class="n">gridWidth</span>
        <span class="n">ringXrightOrigin</span> <span class="o">=</span> <span class="n">ringXrightOrigin</span> <span class="o">+</span> <span class="n">gridWidth</span>

    <span class="c"># Save and close DataSources</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>

    <span class="c">#</span>
    <span class="c"># example run : $ python grid.py &lt;full-path&gt;&lt;output-shapefile-name&gt;.shp xmin xmax ymin ymax gridHeight gridWidth</span>
    <span class="c">#</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span> <span class="p">)</span> <span class="o">!=</span> <span class="mi">8</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;[ ERROR ] you must supply seven arguments: output-shapefile-name.shp xmin xmax ymin ymax gridHeight gridWidth&quot;</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span> <span class="mi">1</span> <span class="p">)</span>

    <span class="n">main</span><span class="p">(</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="convert-polygon-shapefile-to-line-shapefile">
<h2>Convert polygon shapefile to line shapefile<a class="headerlink" href="#convert-polygon-shapefile-to-line-shapefile" title="Permalink to this headline">¶</a></h2>
<p>This recipe converts a poylgon shapefile to a line shapefile</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">os</span>

<span class="k">def</span> <span class="nf">poly2line</span><span class="p">(</span><span class="n">input_poly</span><span class="p">,</span><span class="n">output_line</span><span class="p">):</span>

    <span class="n">source_ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">input_poly</span><span class="p">)</span>
    <span class="n">source_layer</span> <span class="o">=</span> <span class="n">source_ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

    <span class="c"># polygon2geometryCollection</span>
    <span class="n">geomcol</span> <span class="o">=</span>  <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbGeometryCollection</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">source_layer</span><span class="p">:</span>
        <span class="n">geom</span> <span class="o">=</span> <span class="n">feat</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
        <span class="n">ring</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">geomcol</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

    <span class="c"># geometryCollection2shp</span>
    <span class="n">shpDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">output_line</span><span class="p">):</span>
            <span class="n">shpDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">output_line</span><span class="p">)</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="n">shpDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">output_line</span><span class="p">)</span>
    <span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">output_line</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiLineString</span><span class="p">)</span>
    <span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
    <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">geomcol</span><span class="p">)</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">input_poly</span><span class="p">,</span><span class="n">output_line</span><span class="p">):</span>
    <span class="n">poly2line</span><span class="p">(</span><span class="n">input_poly</span><span class="p">,</span><span class="n">output_line</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">input_poly</span> <span class="o">=</span> <span class="s">&#39;test_polygon.shp&#39;</span>
    <span class="n">output_line</span> <span class="o">=</span> <span class="s">&#39;test_line.shp&#39;</span>

    <span class="n">main</span><span class="p">(</span><span class="n">input_poly</span><span class="p">,</span><span class="n">output_line</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="create-point-shapefile-with-attribute-data">
<h2>Create point shapefile with attribute data<a class="headerlink" href="#create-point-shapefile-with-attribute-data" title="Permalink to this headline">¶</a></h2>
<p>This recipe creates a new shapefiles, adds a point to it, and adds a attribute column with a value to it.</p>
<div class="highlight-python"><div class="highlight"><pre>    <span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">os</span>

    <span class="c"># Input data</span>
    <span class="n">pointCoord</span> <span class="o">=</span> <span class="o">-</span><span class="mf">124.4577</span><span class="p">,</span><span class="mf">48.0135</span>
    <span class="n">fieldName</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>
    <span class="n">fieldType</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">OFTString</span>
    <span class="n">fieldValue</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>
    <span class="n">outSHPfn</span> <span class="o">=</span> <span class="s">&#39;test.shp&#39;</span>

    <span class="c"># Create the output shapefile</span>
    <span class="n">shpDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outSHPfn</span><span class="p">):</span>
        <span class="n">shpDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outSHPfn</span><span class="p">)</span>
    <span class="n">outDataSource</span> <span class="o">=</span> <span class="n">shpDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outSHPfn</span><span class="p">)</span>
    <span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">outSHPfn</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span> <span class="p">)</span>

    <span class="c">#create point geometry</span>
    <span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
    <span class="n">point</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">pointCoord</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">pointCoord</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

    <span class="c"># create a field</span>
    <span class="n">idField</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">FieldDefn</span><span class="p">(</span><span class="n">fieldName</span><span class="p">,</span> <span class="n">fieldType</span><span class="p">)</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateField</span><span class="p">(</span><span class="n">idField</span><span class="p">)</span>

    <span class="c"># Create the feature and set values</span>
    <span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
    <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
    <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>
    <span class="n">outFeature</span><span class="o">.</span><span class="n">SetField</span><span class="p">(</span><span class="n">fieldName</span><span class="p">,</span> <span class="n">fieldValue</span><span class="p">)</span>
    <span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
<span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="create-buffer">
<h2>Create buffer<a class="headerlink" href="#create-buffer" title="Permalink to this headline">¶</a></h2>
<p>This recipe buffers features of a layer and saves them to a new Layer</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">os</span>

<span class="k">def</span> <span class="nf">createBuffer</span><span class="p">(</span><span class="n">inputfn</span><span class="p">,</span> <span class="n">outputBufferfn</span><span class="p">,</span> <span class="n">bufferDist</span><span class="p">):</span>
    <span class="n">inputds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">inputfn</span><span class="p">)</span>
    <span class="n">inputlyr</span> <span class="o">=</span> <span class="n">inputds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>

    <span class="n">shpdriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;ESRI Shapefile&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">outputBufferfn</span><span class="p">):</span>
        <span class="n">shpdriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="n">outputBufferfn</span><span class="p">)</span>
    <span class="n">outputBufferds</span> <span class="o">=</span> <span class="n">shpdriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="n">outputBufferfn</span><span class="p">)</span>
    <span class="n">bufferlyr</span> <span class="o">=</span> <span class="n">outputBufferds</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="n">outputBufferfn</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
    <span class="n">featureDefn</span> <span class="o">=</span> <span class="n">bufferlyr</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">feature</span> <span class="ow">in</span> <span class="n">inputlyr</span><span class="p">:</span>
        <span class="n">ingeom</span> <span class="o">=</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
        <span class="n">geomBuffer</span> <span class="o">=</span> <span class="n">ingeom</span><span class="o">.</span><span class="n">Buffer</span><span class="p">(</span><span class="n">bufferDist</span><span class="p">)</span>

        <span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
        <span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">geomBuffer</span><span class="p">)</span>
        <span class="n">bufferlyr</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
        <span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">inputfn</span><span class="p">,</span> <span class="n">outputBufferfn</span><span class="p">,</span> <span class="n">bufferDist</span><span class="p">):</span>
    <span class="n">createBuffer</span><span class="p">(</span><span class="n">inputfn</span><span class="p">,</span> <span class="n">outputBufferfn</span><span class="p">,</span> <span class="n">bufferDist</span><span class="p">)</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">inputfn</span> <span class="o">=</span> <span class="s">&#39;test.shp&#39;</span>
    <span class="n">outputBufferfn</span> <span class="o">=</span> <span class="s">&#39;testBuffer.shp&#39;</span>
    <span class="n">bufferDist</span> <span class="o">=</span> <span class="mf">10.0</span>

    <span class="n">main</span><span class="p">(</span><span class="n">inputfn</span><span class="p">,</span> <span class="n">outputBufferfn</span><span class="p">,</span> <span class="n">bufferDist</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="convert-vector-layer-to-array">
<h2>Convert vector layer to array<a class="headerlink" href="#convert-vector-layer-to-array" title="Permalink to this headline">¶</a></h2>
<p>This recipe converts a vector layer to an array.</p>
<img alt="_images/vector2array.png" src="_images/vector2array.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="p">[[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]</span>
 <span class="p">[</span><span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">0</span><span class="p">]]</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">gdal</span>

<span class="n">vector_fn</span> <span class="o">=</span> <span class="s">&#39;test.shp&#39;</span>

<span class="c"># Define pixel_size and NoData value of new raster</span>
<span class="n">pixel_size</span> <span class="o">=</span> <span class="mi">25</span>
<span class="n">NoData_value</span> <span class="o">=</span> <span class="mi">255</span>

<span class="c"># Open the data source and read in the extent</span>
<span class="n">source_ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">vector_fn</span><span class="p">)</span>
<span class="n">source_layer</span> <span class="o">=</span> <span class="n">source_ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">source_srs</span> <span class="o">=</span> <span class="n">source_layer</span><span class="o">.</span><span class="n">GetSpatialRef</span><span class="p">()</span>
<span class="n">x_min</span><span class="p">,</span> <span class="n">x_max</span><span class="p">,</span> <span class="n">y_min</span><span class="p">,</span> <span class="n">y_max</span> <span class="o">=</span> <span class="n">source_layer</span><span class="o">.</span><span class="n">GetExtent</span><span class="p">()</span>

<span class="c"># Create the destination data source</span>
<span class="n">x_res</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">x_max</span> <span class="o">-</span> <span class="n">x_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">pixel_size</span><span class="p">)</span>
<span class="n">y_res</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">y_max</span> <span class="o">-</span> <span class="n">y_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">pixel_size</span><span class="p">)</span>
<span class="n">target_ds</span> <span class="o">=</span> <span class="n">gdal</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;MEM&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">Create</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">x_res</span><span class="p">,</span> <span class="n">y_res</span><span class="p">,</span> <span class="n">gdal</span><span class="o">.</span><span class="n">GDT_Byte</span><span class="p">)</span>
<span class="n">target_ds</span><span class="o">.</span><span class="n">SetGeoTransform</span><span class="p">((</span><span class="n">x_min</span><span class="p">,</span> <span class="n">pixel_size</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y_max</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">pixel_size</span><span class="p">))</span>
<span class="n">band</span> <span class="o">=</span> <span class="n">target_ds</span><span class="o">.</span><span class="n">GetRasterBand</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">band</span><span class="o">.</span><span class="n">SetNoDataValue</span><span class="p">(</span><span class="n">NoData_value</span><span class="p">)</span>

<span class="c"># Rasterize</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">RasterizeLayer</span><span class="p">(</span><span class="n">target_ds</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">source_layer</span><span class="p">,</span> <span class="n">burn_values</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

<span class="c"># Read as array</span>
<span class="n">array</span> <span class="o">=</span> <span class="n">band</span><span class="o">.</span><span class="n">ReadAsArray</span><span class="p">()</span>
<span class="k">print</span> <span class="n">array</span>
</pre></div>
</div>
</div>
<div class="section" id="convert-polygon-to-points">
<h2>Convert polygon to points<a class="headerlink" href="#convert-polygon-to-points" title="Permalink to this headline">¶</a></h2>
<p>This recipe converts a polygon to points.</p>
<img alt="_images/poly2point1.png" src="_images/poly2point1.png" />
<img alt="_images/poly2point2.png" src="_images/poly2point2.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span><span class="o">,</span> <span class="nn">gdal</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="n">polygon_fn</span> <span class="o">=</span> <span class="s">&#39;test_polygon.shp&#39;</span>

<span class="c"># Define pixel_size which equals distance betweens points</span>
<span class="n">pixel_size</span> <span class="o">=</span> <span class="mi">10</span>

<span class="c"># Open the data source and read in the extent</span>
<span class="n">source_ds</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="n">polygon_fn</span><span class="p">)</span>
<span class="n">source_layer</span> <span class="o">=</span> <span class="n">source_ds</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="n">x_min</span><span class="p">,</span> <span class="n">x_max</span><span class="p">,</span> <span class="n">y_min</span><span class="p">,</span> <span class="n">y_max</span> <span class="o">=</span> <span class="n">source_layer</span><span class="o">.</span><span class="n">GetExtent</span><span class="p">()</span>

<span class="c"># Create the destination data source</span>
<span class="n">x_res</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">x_max</span> <span class="o">-</span> <span class="n">x_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">pixel_size</span><span class="p">)</span>
<span class="n">y_res</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">y_max</span> <span class="o">-</span> <span class="n">y_min</span><span class="p">)</span> <span class="o">/</span> <span class="n">pixel_size</span><span class="p">)</span>
<span class="n">target_ds</span> <span class="o">=</span> <span class="n">gdal</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;GTiff&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">Create</span><span class="p">(</span><span class="s">&#39;temp.tif&#39;</span><span class="p">,</span> <span class="n">x_res</span><span class="p">,</span> <span class="n">y_res</span><span class="p">,</span> <span class="n">gdal</span><span class="o">.</span><span class="n">GDT_Byte</span><span class="p">)</span>
<span class="n">target_ds</span><span class="o">.</span><span class="n">SetGeoTransform</span><span class="p">((</span><span class="n">x_min</span><span class="p">,</span> <span class="n">pixel_size</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">y_max</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">pixel_size</span><span class="p">))</span>
<span class="n">band</span> <span class="o">=</span> <span class="n">target_ds</span><span class="o">.</span><span class="n">GetRasterBand</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">band</span><span class="o">.</span><span class="n">SetNoDataValue</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span>

<span class="c"># Rasterize</span>
<span class="n">gdal</span><span class="o">.</span><span class="n">RasterizeLayer</span><span class="p">(</span><span class="n">target_ds</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">source_layer</span><span class="p">,</span> <span class="n">burn_values</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

<span class="c"># Read as array</span>
<span class="n">array</span> <span class="o">=</span> <span class="n">band</span><span class="o">.</span><span class="n">ReadAsArray</span><span class="p">()</span>

<span class="n">raster</span> <span class="o">=</span> <span class="n">gdal</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">&#39;temp.tif&#39;</span><span class="p">)</span>
<span class="n">geotransform</span> <span class="o">=</span> <span class="n">raster</span><span class="o">.</span><span class="n">GetGeoTransform</span><span class="p">()</span>

<span class="c"># Convert array to point coordinates</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">roadList</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">array</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">multipoint</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPoint</span><span class="p">)</span>
<span class="k">for</span> <span class="n">indexY</span> <span class="ow">in</span> <span class="n">roadList</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
    <span class="n">indexX</span> <span class="o">=</span> <span class="n">roadList</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="n">count</span><span class="p">]</span>
    <span class="n">geotransform</span> <span class="o">=</span> <span class="n">raster</span><span class="o">.</span><span class="n">GetGeoTransform</span><span class="p">()</span>
    <span class="n">originX</span> <span class="o">=</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">originY</span> <span class="o">=</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
    <span class="n">pixelWidth</span> <span class="o">=</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">pixelHeight</span> <span class="o">=</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
    <span class="n">Xcoord</span> <span class="o">=</span> <span class="n">originX</span><span class="o">+</span><span class="n">pixelWidth</span><span class="o">*</span><span class="n">indexX</span>
    <span class="n">Ycoord</span> <span class="o">=</span> <span class="n">originY</span><span class="o">+</span><span class="n">pixelHeight</span><span class="o">*</span><span class="n">indexY</span>
    <span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
    <span class="n">point</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="n">Xcoord</span><span class="p">,</span> <span class="n">Ycoord</span><span class="p">)</span>
    <span class="n">multipoint</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>
    <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>

<span class="c"># Write point coordinates to Shapefile</span>
<span class="n">shpDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&quot;ESRI Shapefile&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">&#39;points.shp&#39;</span><span class="p">):</span>
    <span class="n">shpDriver</span><span class="o">.</span><span class="n">DeleteDataSource</span><span class="p">(</span><span class="s">&#39;points.shp&#39;</span><span class="p">)</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="n">shpDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="s">&#39;points.shp&#39;</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&#39;points.shp&#39;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPoint</span><span class="p">)</span>
<span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>
<span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">multipoint</span><span class="p">)</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>
<span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Remove temporary files</span>
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;temp.tif&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Vector Layers</a><ul>
<li><a class="reference internal" href="#delete-a-file">Delete a file</a></li>
<li><a class="reference internal" href="#is-ogr-installed">Is Ogr Installed</a></li>
<li><a class="reference internal" href="#view-auto-generated-ogr-help">View Auto Generated Ogr Help</a></li>
<li><a class="reference internal" href="#get-list-of-ogr-drivers-alphabetically-a-z">Get List of Ogr Drivers Alphabetically (A- Z)</a></li>
<li><a class="reference internal" href="#is-ogr-driver-available-by-driver-name">Is Ogr Driver Available by Driver Name</a></li>
<li><a class="reference internal" href="#force-ogr-use-named-driver">Force Ogr Use Named Driver</a></li>
<li><a class="reference internal" href="#get-shapefile-feature-count">Get Shapefile Feature Count</a></li>
<li><a class="reference internal" href="#get-all-postgis-layers-in-a-postgresql-database">Get All PostGIS layers in a PostgreSQL Database</a></li>
<li><a class="reference internal" href="#get-postgis-layer-feature-count-by-layer-name">Get PostGIS Layer Feature Count By Layer Name</a></li>
<li><a class="reference internal" href="#get-all-layers-in-an-esri-file-geodatabase">Get all layers in an Esri File GeoDataBase</a></li>
<li><a class="reference internal" href="#load-data-to-memory">Load data to memory</a></li>
<li><a class="reference internal" href="#iterate-over-features">Iterate over Features</a></li>
<li><a class="reference internal" href="#get-geometry-from-each-feature-in-a-layer">Get Geometry from each Feature in a Layer</a></li>
<li><a class="reference internal" href="#filter-by-attribute">Filter by attribute</a></li>
<li><a class="reference internal" href="#spatial-filter">Spatial Filter</a></li>
<li><a class="reference internal" href="#get-shapefile-fields-get-the-user-defined-fields">Get Shapefile Fields - Get the user defined fields</a></li>
<li><a class="reference internal" href="#get-shapefile-fields-and-types-get-the-user-defined-fields">Get Shapefile Fields and Types - Get the user defined fields</a></li>
<li><a class="reference internal" href="#get-postgis-layer-fields-get-the-user-defined-fields">Get PostGIS Layer Fields - Get the user defined fields</a></li>
<li><a class="reference internal" href="#get-postgis-layer-fields-and-types-get-the-user-defined-fields">Get PostGIS Layer Fields and Types - Get the user defined fields</a></li>
<li><a class="reference internal" href="#get-a-layer-s-capabilities">Get a Layer&#8217;s Capabilities</a></li>
<li><a class="reference internal" href="#get-wfs-layers-and-iterate-over-features">Get WFS layers and iterate over features</a></li>
<li><a class="reference internal" href="#set-http-proxy-options-before-fetching-a-web-datasource">Set HTTP Proxy options before fetching a web datasource</a></li>
<li><a class="reference internal" href="#read-a-csv-of-coordinates-as-an-ogrvrtlayer">Read a CSV of Coordinates as an OGRVRTLayer</a></li>
<li><a class="reference internal" href="#create-a-new-layer-from-the-extent-of-an-existing-layer">Create a new Layer from the extent of an existing Layer</a></li>
<li><a class="reference internal" href="#save-the-convex-hull-of-all-geometry-from-an-input-layer-to-an-output-layer">Save the convex hull of all geometry from an input Layer to an output Layer</a></li>
<li><a class="reference internal" href="#save-centroids-of-input-layer-to-an-output-layer">Save centroids of input Layer to an output Layer</a></li>
<li><a class="reference internal" href="#create-a-new-shapefile-and-add-data">Create a New Shapefile and Add Data</a></li>
<li><a class="reference internal" href="#create-a-postgis-table-from-wkt">Create a PostGIS table from WKT</a></li>
<li><a class="reference internal" href="#filter-and-select-input-shapefile-to-new-output-shapefile-like-ogr2ogr-cli">Filter and Select Input Shapefile to New Output Shapefile Like ogr2ogr CLI</a></li>
<li><a class="reference internal" href="#merge-ogr-layers">Merge OGR Layers</a></li>
<li><a class="reference internal" href="#get-a-list-of-the-street-names-in-a-osm-file">Get a list of the street names in a OSM file</a></li>
<li><a class="reference internal" href="#create-fishnet-grid">Create fishnet grid</a></li>
<li><a class="reference internal" href="#convert-polygon-shapefile-to-line-shapefile">Convert polygon shapefile to line shapefile</a></li>
<li><a class="reference internal" href="#create-point-shapefile-with-attribute-data">Create point shapefile with attribute data</a></li>
<li><a class="reference internal" href="#create-buffer">Create buffer</a></li>
<li><a class="reference internal" href="#convert-vector-layer-to-array">Convert vector layer to array</a></li>
<li><a class="reference internal" href="#convert-polygon-to-points">Convert polygon to points</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="geometry.html"
                        title="previous chapter">Geometry</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="raster_layers.html"
                        title="next chapter">Raster Layers</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/vector_layers.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="raster_layers.html" title="Raster Layers"
             >next</a> |</li>
        <li class="right" >
          <a href="geometry.html" title="Geometry"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2013, Jared Erickson, Cort Daniel, Michael Payne.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
</html>